//
//  CZPopMenu.m
//  OCKSFWeibo
//
//  Created by 盛 发 况 on 16/4/18.
//  Copyright © 2016年 盛 发 况. All rights reserved.
//

#import "CZPopMenu.h"

@implementation CZPopMenu

/**
 *  显示弹出菜单
 */
+ (instancetype)showInRect:(CGRect)rect {

    CZPopMenu *menu = [[CZPopMenu alloc] initWithFrame:rect];
    menu.userInteractionEnabled = YES;
    menu.image = [UIImage imageWithStretchableName:@"popover_background"];
    [CZKeyWindow addSubview:menu];

    return menu;
}

/**
 *  隐藏弹出菜单
 */
+ (void)hide {

    for (UIView *popMenu in CZKeyWindow.subviews) {
        if ([popMenu isKindOfClass:self]) {
            [popMenu removeFromSuperview];
        }
    }
}

// 内容视图
- (void)setContentView:(UIView *)contentView {
    // 先移除之前内容视图
    [_contentView removeFromSuperview];
    
    _contentView = contentView;
    contentView.backgroundColor = [UIColor clearColor];
    
    [self addSubview:contentView];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    // 计算内容视图尺寸
    CGFloat margin = 5;
    CGFloat x = margin;
    CGFloat y = 9;
    CGFloat w = self.width - 2 * margin;
    CGFloat h = self.height - y - margin;

    _contentView.frame = CGRectMake(x, y, w, h);
}

@end




